package edu.umn.contactviewer;

import java.util.ArrayList;
import java.util.Map;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Displays a list of contacts.
 * 
 */
public class ContactListActivity extends ListActivity {

	public static final int SHOW_CONTACT = 99;
	public static final int ADD_EDIT_CONTACT = 100;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		ToolbarConfig config = new ToolbarConfig(this, "Contacts");

		Button b = ((Button) config.getActivity().findViewById(
				R.id.toolbar_add_edit_button));
		b.setText(R.string.add);
		b.setVisibility(View.VISIBLE);
		b.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(ContactListActivity.this,
						AddEditContactActivity.class);

				intent.putExtra("selected-contact", new Contact());
				intent.putExtra("isAdd", true);

				startActivityForResult(intent, ADD_EDIT_CONTACT);

			}
		});

		// initialize the list view
		setListAdapter(new ContactAdapter(this, R.layout.list_item,
				ContactsDB.getContacts(this)));
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		// handle the item click events
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Intent intent = new Intent(ContactListActivity.this,
						ContactSelectedActivity.class);

				intent.putExtra("selected-contact",
						((ContactAdapter) getListAdapter()).getItem(position));

				startActivityForResult(intent, SHOW_CONTACT);

			}
		});

	}

	/*
	 * We need to provide a custom adapter in order to use a custom list item
	 * view.
	 */
	public class ContactAdapter extends ArrayAdapter<Contact> {

		public ContactAdapter(Context context, int textViewResourceId,
				Map<String, Contact> objects) {
			super(context, textViewResourceId, new ArrayList<Contact>(
					objects.values()));
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = getLayoutInflater();
			View item = inflater.inflate(R.layout.list_item, parent, false);

			Contact contact = getItem(position);
			((TextView) item.findViewById(R.id.item_name)).setText(contact
					.getName());
			((TextView) item.findViewById(R.id.item_title)).setText(contact
					.getTitle());
			((TextView) item.findViewById(R.id.item_phone)).setText(contact
					.getPhone());

			return item;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent returnedIntent) {
		super.onActivityResult(requestCode, resultCode, returnedIntent);

		if (resultCode == SHOW_CONTACT || resultCode == ADD_EDIT_CONTACT) {

			setListAdapter(new ContactAdapter(this, R.layout.list_item,
					ContactsDB.getContacts(this)));
		}

	}

}
